<template>
  <div>
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="dataList"
      :options="options"
      :rowHandle="rowHandle"
      @add-model="add"
      @edit-model="mod"
      @del-model="del"
      index-row
      selection-row
      @row-click="rowClick"
      @selection-change="selectChange"
      :pagination="pagination"
      @pagination-current-change="pageChange">
      <div class="title" slot="header">
        <span class="text">基础示例</span>
        <head-btns :add="add" :mod="mod" :del="del"/>
      </div>
    </d2-crud>
    <detail-info v-model="detailVisible" ref="detailFrom" @submit="_fetchListData"/>
  </div>
</template>
<script>
  import detailInfo from "./detail-info";
  import discretionApi from "@/api/system/discretion";
  import utils from '@/assets/utils/zhzf-util';
  import dictCache from '@/assets/utils/cache-dict-util';
  import HeadBtns from "@/views/comps/zhzf/head-btns";

  export default {
    components: {HeadBtns, detailInfo},
    data() {
      return {
        dataList: [],
        detailVisible: false,
        columns: [
          {title: '违法行为', key: 'name'},
          {title: '违法情节', key: 'code'},
          {title: '违法程度', key: 'remark'},
          {title: '上限', key: 'remark'},
          {title: '下限', key: 'remark'},
          {title: '触发标准', key: 'remark'},
          {title: '备注', key: 'remark'},
        ],
        rowHandle: {
          custom: [
            {title: '修改', size: 'mini', emit: 'edit-model', icon: "el-icon-edit", style: "color:#67c23a;"},
            {title: '删除', size: 'mini', emit: 'del-model', icon: "el-icon-delete", style: "color:#f56c6c;"}
          ]
        },
        options: {
          stripe: true,
          highlightCurrentRow: true
        },
        pagination: {
          currentPage: 1,
          pageSize: 20,
          total: 0
        },
        queryParam: {},
        selected: [],
      };
    },
    mounted() {
      this.initPage();
    },
    methods: {
      async initPage() {
        await this._fetchListData();
      },
      async _fetchListData() {
        this.dataList = await discretionApi.pageList({...this.pagination});
        this.pagination.total = this.dataList.length;
      },
      pageChange(currentPage) {
        this.pagination.currentPage = currentPage;
        this._fetchListData();
      },
      rowClick(row) {
        this.$refs.d2Crud.$children[1].toggleRowSelection(row);
      },
      selectChange(selection) {
        this.selected = selection;
      },
      showDetail(row) {
        this.detailVisible = true;
        this.$refs.detailFrom.initPage(row);
      },
      add(rd) {
        this.showDetail();
      },
      mod(rd) {
        if (rd) {
          this.showDetail(rd.row);
        } else if (this.selected.length == 1) {
          this.showDetail(this.selected[0]);
        } else {
          utils.showTips('请选择一行数据');
          return;
        }
      },
      del(rd) {
        if (!rd && !this.selected.length) {
          utils.showTips('请选择要删除的数据', false);
          return;
        }
        let delIds = [];
        if (rd) {
          delIds = [rd.row.id];
        } else {
          this.selected.map(item => {
            delIds.push(item.id)
          });
        }

        this.$confirm("确认删除？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        }).then(() => {
          this.submitDel(delIds)
        });
      },
      async submitDel(ids) {
        await discretionApi.del(ids);
        await this.initPage();
      }
    }
  };
</script>

<style lang="scss">
</style>
